package top.oneit.jdownload.config;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import okhttp3.Headers;
import okhttp3.Request;
import org.hibernate.Session;
import top.oneit.jdownload.JdownloadApplication;
import top.oneit.jdownload.data.dao.AppConfigDao;
import top.oneit.jdownload.data.entity.AppConfig;

import java.util.List;
import java.util.Map;

/**
 * @author lingkang
 * @date 2021/11/20
 */
public class ApplicationConfig {
    public static AppConfig getAppConfig() {
        Session session = JdownloadApplication.sessionFactory.getCurrentSession();
        session.beginTransaction();
        List<AppConfig> list = session.createQuery("from AppConfig order by id desc").setMaxResults(1).list();
        session.getTransaction().commit();
        return list.get(0);
    }

    public static void init(Session session) {
        List<AppConfig> list = AppConfigDao.getList(AppConfig.class);
        if (CollUtil.isNotEmpty(list)) {
            return;
        }
        // 默认值！
        AppConfigDao.save(getDefaultAppConfig());
    }

    public static AppConfig getDefaultAppConfig() {
        AppConfig config = new AppConfig();
        config.setId(1L);
        config.setVersion("1.0.0");
        config.setDefaultThreadNumber(32);
        config.setDefaultRequestHeader(getDefaultRequestToJsonString());
        return config;
    }

    public static JSONObject getDefaultRequestToJson() {
        JSONObject json = new JSONObject();
        json.put("my-web", "lingkang.top");
        json.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
        json.put("Accept-Encoding", "gzip, deflate");
        json.put("Accept-Language", "zh-CN,zh;q=0.9");
        json.put("Connection", "keep-alive");
        json.put("Upgrade-Insecure-Requests", "1");
        json.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36");
        return json;
    }

    public static String getDefaultRequestToJsonString() {
        return getDefaultRequestToJson().toJSONString();
    }

    public static Headers getDefaultHeaders() {
        return getDefaultHeaders(null);
    }

    public static Headers getDefaultHeaders(String Referer) {
        Object requestHeader = JdownloadApplication.cache.get("requestHeader");
        if (ObjectUtil.isEmpty(requestHeader)) {
            JSONObject head = getDefaultRequestToJson();
            Request.Builder builder = new Request.Builder();
            builder.url("http://url");
            for (Map.Entry entry : head.entrySet()) {
                builder.addHeader(entry.getKey().toString(), entry.getValue().toString());
            }
            if (Referer != null) {
                builder.addHeader("Referer", Referer);
            }
            Headers headers = builder.build().headers();
            JdownloadApplication.cache.put("requestHeader", builder.build().headers());
            return headers;
        }
        return (Headers) requestHeader;
    }
}
